package cn.cqvie.pojo;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;

@Data
public class Employee implements Serializable,UserDetails {

    private static final Long serialVersionID=1L;

    private Long id;

    private String username;

    private String password;

    private String name;

    private Integer sex;

    private String phone;

    private String idNumber;

    @TableField(fill = FieldFill.INSERT)
    private boolean isEnabled;

    @TableField(fill = FieldFill.INSERT)
    private boolean isAccountNonExpired;

    @TableField(fill = FieldFill.INSERT)
    private boolean isAccountNonLocked;

    @TableField(fill = FieldFill.INSERT)
    private boolean isCredentialsNonExpired;

    @TableField(fill = FieldFill.INSERT)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime updateTime;

    @TableField(fill = FieldFill.INSERT)
    private Long createUser;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateUser;


    @TableField(exist = false)
    private List<Role> roles = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<SimpleGrantedAuthority> grantedAuthorities = new HashSet<>();
        roles.forEach(item->{
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(item.getName());
            grantedAuthorities.add(simpleGrantedAuthority);
        });
       return grantedAuthorities;

    }
}
